package top.likeqc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.likeqc.common.lang.Result;
import top.likeqc.dto.CourseStudentDTO;
import top.likeqc.entity.CourseStudent;
import top.likeqc.service.ICourseStudentService;
import top.likeqc.vo.StudentVO;

/**
 * <p>
 * 选课（课程-学生） 前端控制器
 * </p>
 *
 * @author likeqc
 * @since 2022-04-11
 */
@RestController
@RequestMapping("/courseStudent")
public class CourseStudentController extends BaseController {

    @Autowired
    ICourseStudentService courseStudentService;

    @GetMapping()
    public Result getStudentListByCourseID(Long courseId) {
        Page<StudentVO> page = courseStudentService.getStudentListByCourseID(getPage(), courseId);

        return Result.success(page);
    }

    @PostMapping()
    public Result addCourseStudent(@RequestBody CourseStudentDTO courseStudentDTO) {
        delete(courseStudentDTO);
        courseStudentService.saveBatch(courseStudentDTO.getCourseStudentList());
        return Result.success();
    }

    @DeleteMapping()
    public Result delete(@RequestBody CourseStudentDTO courseStudentDTO) {
        courseStudentService.remove(new QueryWrapper<CourseStudent>().eq("course_id", courseStudentDTO.getCourseId())
            .in("student_id", courseStudentDTO.getNumber()));
        return Result.success();
    }


}
